package toanstt.com.MatchUp;





import toanstt.com.ads.Ads;
import toanstt.com.gameplay.G;
import toanstt.com.menu.HowTo;
import toanstt.com.menu.M;
import toanstt.com.menu.MenuNewGame;
import toanstt.com.menu.Setting;
import toanstt.com.menu.Thumbs;
import android.content.Context;
import android.graphics.Canvas;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class App extends SurfaceView implements Runnable
{
	public static long currenttime;
	long lasttime;
	Thread t=null;
	SurfaceHolder holder;
	boolean isItOK=false;
	static int iscreen = 0;
	int gamegloaded = -1;
	G gamemanagement;
	M m;
	MenuNewGame menunewgame;
	Setting setting;
	HowTo howto;
	public static Ads ads;
	public static Thumbs thumbs;
	
	public App(Context context, AttributeSet aSet) 
	{
		super(context, aSet);
		holder = getHolder();
		gamegloaded =30;
		
	}
	@Override
	public void run() 
	{
		while(isItOK == true)
		{
			if(gamegloaded !=-1 )
			{
				currenttime = System.currentTimeMillis();
				if(currenttime- lasttime < 50)
					Thread.yield();
				else
				{
					lasttime = currenttime;
					if(!holder.getSurface().isValid())
						continue;
					Canvas c = holder.lockCanvas();
					//loading.Draw(c);
					if(gamegloaded>=20)
					{
						T.paint_alpha.setAlpha((10-gamegloaded+20)*25);
						c.drawBitmap(T.BITMAP_BLASH.bitmap[0], 0, T.DISPLAY_HEIGHT/2 - T.BITMAP_BLASH.bitmap[0].getHeight()/2, T.paint_alpha);
						//if(c!=null)
						//c.drawARGB(100, 0, 0, 255);
						holder.unlockCanvasAndPost(c);
						gamegloaded--;
						continue;
					}
					
					//holder.unlockCanvasAndPost(c);
					if(gamegloaded==19)
					{
						c.drawBitmap(T.BITMAP_BLASH.bitmap[0], 0, T.DISPLAY_HEIGHT/2 - T.BITMAP_BLASH.bitmap[0].getHeight()/2, null);
						ads = new Ads(MainActivity.adView);
						T.Load(MainActivity.activity);
						T.notice = new Notice();
						gamemanagement = new G();
						m = new M();
						menunewgame = new MenuNewGame();
						setting = new Setting();
						howto = new HowTo();
						thumbs = new Thumbs();
						T.SCREEN = SCR.MENU;
						//G.NewGame(0, 0);
						//gamegloaded = -1;
						T.soundtheme.Play();
						holder.unlockCanvasAndPost(c);
						gamegloaded--;
						continue;
					}
					if(gamegloaded>10)
					{
						gamegloaded--;
						holder.unlockCanvasAndPost(c);
						continue;
					}
					if(gamegloaded>=0)
					{
						c.drawARGB(255, 0, 0, 0);
						T.paint_alpha.setAlpha((gamegloaded)*25);
						c.drawBitmap(T.BITMAP_BLASH.bitmap[0], 0,T.DISPLAY_HEIGHT/2 - T.BITMAP_BLASH.bitmap[0].getHeight()/2, T.paint_alpha);
						gamegloaded--;
						holder.unlockCanvasAndPost(c);
						T.ALPHA = 10;
						//T.SCREEN = SCR.GAME;
						//G.NewGame(0, 0);
						continue;
					}
					
				}
			}
			else
			{
				currenttime = System.currentTimeMillis();
				if(currenttime- lasttime < 50)
					Thread.yield();
				else
				{
					lasttime = currenttime;
					if(!holder.getSurface().isValid())
						continue;
					Update();
					Canvas c = holder.lockCanvas();
					c.drawARGB(255, 200, 200, 200);
					mainDraw(c);
					holder.unlockCanvasAndPost(c);
				}
			}
		}
	}
	public void Update()
	{
		
	}
	public void mainDraw(Canvas canvas)
	{
		if(T.IS_BUSY)
		{
			canvas.drawARGB(255, 0, 0, 0);
		}
		else
		switch(T.SCREEN)
		{
		
		case SCR.MENU:
			//mainmenu.Draw(canvas);
			m.Draw(canvas);
			break;
		case SCR.GAME:
			gamemanagement.Draw(canvas);
			break;
		case SCR.SETTING:
			//gopy.touch(mo);
			setting.Draw(canvas);
			break;
		case SCR.NEWGAME:
			menunewgame.Draw(canvas);
			//shop.Draw(canvas);
			break;
		case SCR.THUMB:
			thumbs.Draw(canvas);
			break;
		case SCR.HUONGDAN:
			howto.Draw(canvas);
		break;		
			
		
		}
		
		
		if(T.IS_BUSY)
		{
			canvas.drawARGB(255, 0, 0, 0);
		}
		
		if(T.onnotice==true)
		{
			T.notice.Draw(canvas);
		}
		
		if(T.ALPHA > 10)
		{
			canvas.drawARGB((20-T.ALPHA) *25, 0, 0, 0);
			T.ALPHA--;
			if(T.ALPHA == 10 && T.SCREEN_TEMP>=0 )
			{
				
				{
					T.SCREEN = T.SCREEN_TEMP;
					T.SCREEN_TEMP = -1;
					T.onnotice = false;
				}
			}
			
				
		}
		else if(T.ALPHA > 0)
		{
			if(T.ALPHA == 9 && T.GAMEPLAYMODE>0 )
			{
				if(T.GAMEPLAYMODE==1)
				{
					G.savegame.LoadGame();
					T.onnotice = false;
				}
				else if(T.GAMEPLAYMODE==2)
				{
					G.NewGame(T.GAME_I1, T.GAME_I2);
					T.onnotice = false;
				}
					
				T.SCREEN_TEMP = -1;
				T.SCREEN = SCR.GAME;
				T.GAMEPLAYMODE=0;
			}
			{
				canvas.drawARGB(T.ALPHA *25, 0, 0, 0);
				T.ALPHA--;
			}
		}
		else if(T.ALPHA == 0 )
		{
			
			if(T.SCREEN_TEMP != -1)
				T.SCREEN_TEMP = -1;
		}
		
		
		
		ads.Draw(canvas);
		
	}
	long time_temp;
	public void pause()
	{
		time_temp = System.currentTimeMillis();
		//Log.d("pasue","1111111111111");
		isItOK = false;
		while(true)
		{
			try
			{
				t.join();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			break;
		}
		t=null;
	}
	public void resume()
	{
		T.DELAY_TIME += (System.currentTimeMillis() - time_temp);
		//Log.d("resume","2222222222222222");
		//if(gamemanagement != null)
		//{
			//gamemanagement.resume();
		//}
		isItOK = true;
		t = new Thread(this);
		t.start();
		
	}
	public static int temp_x,temp_y;
	public static int action, actionCode;
	public void touch(MotionEvent mo)
	{
		
		if(gamegloaded == -1)
		{
			if(ads.touch(mo) ==false)
			{
				action = mo.getAction();
				actionCode = action & MotionEvent.ACTION_MASK;
				temp_x = (int)mo.getX();
				temp_y = (int)mo.getY();
				//if(ads.touch(mo) == false)
				{
					if(T.onnotice==true)
					{
						T.notice.toch(mo);
					}
					else
					switch(T.SCREEN)
					{
					case SCR.MENU:
						m.touch(mo);
						//mainmenu.touch(mo);
						break;
					case SCR.GAME:
						gamemanagement.touch(mo);
						break;
					case SCR.NEWGAME:
						menunewgame.touch(mo);
						//shop.touch(mo);
						break;
					case SCR.THUMB:
						thumbs.touch(mo);
						break;
					case SCR.SETTING:
						//gopy.touch(mo);
						setting.touch(mo);
						break;
					case SCR.HUONGDAN:
						howto.touch(mo);
						//huongdan.touch(mo);
					break;		
					
					}
				}
			}
			
		}
	
	}
}
